<template>
  <div class="app-container-custom">
    <el-row :gutter="10">
      <fold :span="5" :xs="24" :show.sync='sideshow'>
        <el-card shadow="never">
          <org-tree ref="orgTree" modelType="'dept','community','floor','unit','room'" @nodeClick="handleNodeClick"
                    @firstNodeClick="handleNodeClick"/>
        </el-card>
      </fold>
      <el-col :span="sideshow ? 19 : 24" :xs="24">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain size="mini" @click="handleAddFloor" v-hasPermi="['property:floor:add']">新增楼栋</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" plain size="mini" @click="handleAddUnit" v-hasPermi="['property:unit:add']">新增单元</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" plain size="mini" @click="handleAddLayer">新增楼层</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" plain size="mini" @click="handleAddRoom" v-hasPermi="['property:room:add']">新增房屋</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" plain size="mini" @click="handleBatchRoom" v-hasPermi="['property:room:batchUpdateRoom']">批量修改房屋信息</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain size="mini" @click="handleDelete" v-hasPermi="['property:room:remove']">删除</el-button>
          </el-col>
          <!--<el-col :span="1.5">-->
            <!--<el-button type="primary" plain size="mini" @click="handleInitHouse">快速初始化</el-button>-->
          <!--</el-col>-->
        </el-row>
        <el-card shadow="never">
          <community-main res="communityRefs" v-if="currentNode.modelType == 'community'" :currentNode="currentNode"/>
          <floor-main res="floorRefs" v-if="currentNode.modelType == 'floor'" :currentNode="currentNode"/>
          <unit-form res="unitRefs" v-if="currentNode.modelType == 'unit'" :currentNode="currentNode"/>
          <room-main res="unitRefs" v-if="currentNode.modelType == 'room'" :currentNode="currentNode"/>
        </el-card>
      </el-col>
    </el-row>
    <floor-add-form ref="addFloorRefs" @ok="$refs.orgTree.refresh()"/>
    <unit-add-form ref="addUnitRefs" @ok="$refs.orgTree.refresh()"/>
    <room-add-form ref="addRoomRefs" @ok="$refs.orgTree.refresh()"/>
    <room-add-layer-form ref="addLayerRefs" @ok="$refs.orgTree.refresh()"/>
    <batch-room-form ref="batchRoomRef" @ok="$refs.orgTree.refresh()" />
  </div>
</template>

<script>
  import { houseMangeMixin } from "./houseMangeMixin"
  import CommunityMain from "./community/main";
  import FloorMain from "./floor/main";
  import FloorAddForm from "./floor/addForm";
  import UnitForm from "./unit/form";
  import UnitAddForm from "./unit/addForm";
  import RoomMain from "./room/main";
  import RoomAddForm from "./room/addForm";
  import RoomAddLayerForm from "./room/addLayerForm";
  import BatchRoomForm from "./room/batchForm";
  import { delFloor } from "@/api/property/floor";
  import { delRoom } from "@/api/property/room";
  import { delUnit} from "@/api/property/unit";

  export default {
    name: "HouseMange",
    mixins:[ houseMangeMixin ],
    components:{ CommunityMain,FloorMain ,UnitForm,RoomMain,FloorAddForm,UnitAddForm,RoomAddForm,RoomAddLayerForm,BatchRoomForm},
    data() {
      return {
        currentNode:{}
      }
    },
    methods:{
      handleNodeClick(node){
        this.currentNode = node;
      },
      handleAddFloor(){
        if(this.currentNode.modelType=='community'){
          this.$refs.addFloorRefs.show({currentNode:this.currentNode})
        }else{
          this.msgWarning("请选择所属小区")
        }
      },
      handleAddUnit(){
        if(this.currentNode.modelType=='floor'){
          this.$refs.addUnitRefs.show({currentNode:this.currentNode})
        }else{
          this.msgWarning("请选择所属楼栋")
        }
      },
      handleAddLayer(){
        if(this.currentNode.modelType=='unit'){
          this.$refs.addLayerRefs.show({currentNode:this.currentNode})
        }else{
          this.msgWarning("请选择所属单元")
        }
      },
      handleAddRoom(){
        if(this.currentNode.modelType=='room'){
          this.$refs.addRoomRefs.show({currentNode:this.currentNode})
        }else{
          this.msgWarning("请选择任意房间号")
        }
      },
      handleDelete(){
        const ids = this.currentNode.id;
        if(this.currentNode.modelType=='floor'){
          this.$confirm('是否确认删除楼栋为"' + this.currentNode.label + '"的数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return delFloor(ids);
          }).then(() => {
            this.$refs.orgTree.refresh()
            this.msgSuccess("删除楼栋成功");
          }).catch(() => {});
        }else if(this.currentNode.modelType=='unit'){
          this.$confirm('是否确认删除单元为"' + this.currentNode.label + '"的数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return delUnit(ids);
          }).then(() => {
            this.$refs.orgTree.refresh()
            this.msgSuccess("删除单元成功");
          }).catch(() => {});
        }else if(this.currentNode.modelType=='room'){
          this.$confirm('是否确认删除房间为"' + this.currentNode.label + '"的数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return delRoom(ids);
          }).then(() => {
            this.$refs.orgTree.refresh()
            this.msgSuccess("删除房间成功");
          }).catch(() => {});
        }else{
          this.msgWarning("只能删除楼栋、单元、房间。")
        }
      },
      handleBatchRoom(){
        if(this.currentNode.modelType=='community' || this.currentNode.modelType=='floor' || this.currentNode.modelType=='unit' || this.currentNode.modelType=='room'){
          this.$refs.batchRoomRef.show({currentNode:this.currentNode})
        }else{
          this.msgWarning("请选择所属小区/楼栋/单元/房屋")
        }
      },
      handleInitHouse(){
        if(this.currentNode.modelType=='community'){

        }else{
          this.msgWarning("请选择所属小区")
        }
      }
    }
  }
</script>

<style scoped>

</style>
